import { TitleDisplayOptions } from "@minecraft/server";
import { GameComponent } from "@sapi-game/main";
import { WereWolfMorningPhase } from "@/werewolf/phases/morning";
import { WereWolfStatus } from "@/werewolf/player";

export class MorningClassicView extends GameComponent<WereWolfMorningPhase> {
    get players() {
        return this.context.players;
    }

    override onAttach(): void {
        const players = this.context.players;
        this.bell();
        this.runner.run(async (r) => {
            players.title(`§e§l第${this.context.day}天`);
            await r.wait(60);
            players.title("§e§l请来到中间场地集合");
            await r.wait(60);
            this.news();
        });
    }

    bell() {
        this.runner.run(async (r) => {
            this.players.playSound("block.bell.hit", { volume: 0.8 });
            await r.wait(8);
            this.players.playSound("block.bell.hit", { volume: 0.8 });
            await r.wait(8);
            this.players.playSound("block.bell.hit", { volume: 0.8 });
        });
    }

    news() {
        this.runner.run(async (r) => {
            this.players.playSound("note.bell", { pitch: 0.6, volume: 0.8 });
            await r.wait(3);
            this.players.playSound("note.bell", { pitch: 0.7, volume: 0.8 });
            await r.wait(5);
            this.players.playSound("note.bell", { pitch: 0.8, volume: 0.8 });
        });
        const options: TitleDisplayOptions = {
            fadeInDuration: 0,
            stayDuration: 90,
            fadeOutDuration: 0,
        };
        this.runner.run(async (r) => {
            this.players.title("§e§l晨报", undefined, options);
            await r.wait(4);
            this.players.title("§e§l=晨报=", undefined, options);
            await r.wait(4);
            this.players.title("§e§l==晨报==", undefined, options);
            await r.wait(5);
            this.players.title("§e§l-==晨报==-", undefined, options);
            await r.wait(4);
            this.players.title("§e§l--==晨报==--", undefined, options);
            let text = "§l§e昨天晚上";
            for (let i = 0; i < 4; i++) {
                await r.wait(10);
                text += ".";
                this.players.title(text);
            }
            await r.wait(20);
            const deadPlayers = this.players.filter(
                (p) => p.status == WereWolfStatus.justDead
            );
            if (deadPlayers.length) {
                this.players.title(
                    "§l§0[§7死亡§0]§4" +
                        deadPlayers.map((p) => p.name).join(",")
                );
                await r.wait(20);
                this.players.forEach((p) =>
                    p.player.onScreenDisplay.updateSubtitle("§4§l去世了")
                );
            } else {
                this.players.title("§a§l是平安夜");
            }
            await r.wait(20);
            this.state.next();
        });
    }
}
